import 'package:easy_refresh/easy_refresh.dart';
import 'package:enter_prise_web/common/constants.dart';
import 'package:enter_prise_web/models/user.dart';
import 'package:enter_prise_web/page/staff/group_list.dart';
import 'package:enter_prise_web/page/index/index_controller.dart';
import 'package:enter_prise_web/page/settings/setting_controller.dart';
import 'package:enter_prise_web/page/staff/staff_controller.dart';
import 'package:enter_prise_web/page/staff/staff_setting.dart';
import 'package:enter_prise_web/widgets/page/page_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';

enum QueryStaffType { all, group, uid }

class Staff extends StatefulWidget {
  final String? gid;

  const Staff({super.key, this.gid});

  @override
  State<Staff> createState() => _StaffState();
}

class _StaffState extends State<Staff> {
  final pageControllers = Get.find<PageControllers>();
  final sController = Get.find<settingController>();
  final IndexController c = Get.find();
  final staffController dx = Get.find();
  final EasyRefreshController refreshController = EasyRefreshController(
      controlFinishLoad: true, controlFinishRefresh: true);
  int selectCount = 0;
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10)),
            padding: const EdgeInsets.only(
              top: 10,
              bottom: 10,
              left: 10,
              right: 10,
            ),
            child: GetBuilder<staffController>(
              id: 'group_list',
              builder: (controller) {
              return ListView.builder(
                itemCount: controller.groups.length,
                itemBuilder: (context, index) {
                  return buildGroupItem(users: controller.groups[index]);
                },
              );
            })),
      ],
    );
  }
}


class buildGroupItem extends StatefulWidget {
  final Map<String, dynamic>? users;
  const buildGroupItem({super.key, this.users});

  @override
  State<buildGroupItem> createState() => _buildGroupItemState();
}

class _buildGroupItemState extends State<buildGroupItem> {
  bool _isExpanded = false;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        GestureDetector(
          onLongPress: () {
            //跳转到分组管理
            showCupertinoModalBottomSheet(
                context: context,
                builder: (context) {
                  return SizedBox(
                    height: 150,
                    child: Column(
                      children: [
                        _buildRowforBottomSheet(
                            text: "分组管理",
                            onTap: () {
                              Get.to(const GroupListIndex(
                                isMove: false,
                                groupName: '',
                              ));
                            }),
                        const Divider(),
                        _buildRowforBottomSheet(
                          text: "取消",
                          onTap: () => Get.back(),
                        ),
                      ],
                    ),
                  );
                });
          },
          onTap: () {
            _isExpanded = !_isExpanded;
            setState(() {});
          },
          child: Padding(
            padding:
                const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
            child: Row(
              children: [
                Icon(
                  _isExpanded ? Icons.arrow_drop_down : Icons.arrow_right,
                  color: kGrayColor,
                  size: 40,
                ),
                const SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: Text(
                    widget.users!['name'],
                    style: const TextStyle(
                      fontSize: 20,
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
        _buildSubGroupItem(widget.users!['list'], widget.users!['name'])
      ],
    );
  }

  _buildRowforBottomSheet({VoidCallback? onTap, String? text}) {
    return GestureDetector(
      onTap: () {
        onTap!();
      },
      child: SizedBox(
        width: double.infinity,
        height: 60,
        child: Center(
          child: Text(text ?? "",
              style: const TextStyle(
                  color: Colors.black,
                  fontSize: 30,
                  decoration: TextDecoration.none)),
        ),
      ),
    );
  }

  _buildSubGroupItem(List userList, groupName) {
    Widget subWidget;
    if (!_isExpanded) {
      subWidget = Container();
    } else {
      subWidget = SizedBox(
          width: double.infinity,
          height: 72.0 * userList.length,
          child: _createColumnByUserList(userList, groupName));
    }
    return subWidget;
  }

  _createColumnByUserList(List userList, groupName) {
    return Column(
      children: userList.map((e) {
        return _subCategoryItem(e, groupName);
      }).toList(),
    );
  }

  Widget _subCategoryItem(maps, groupName) {
    Users users = Users(
        uid: maps['uid'],
        username: maps['name'],
        password: maps['password'],
        groupName: groupName,
        gid: maps['gid'].toString());
    return GestureDetector(
      onTap: () {
        Get.to(StaffSetting(
          user: users,
        ));
      },
      child: Container(
        margin: const EdgeInsets.only(left: 8.0, right: 8.0, bottom: 12.0),
        height: 60,
        child: Row(children: <Widget>[
          const SizedBox(width: 40.0),
          CircleAvatar(
            backgroundImage: NetworkImage(
              "http://flycan.ren:8026/avatar/getByUid?uid=${users.uid}",
            ),
          ),
          const SizedBox(width: 10.0),
          Column(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
            Text(
              users.username!,
              style: const TextStyle(fontSize: 19, fontWeight: FontWeight.w500),
            ),
          ])
        ]),
      ),
    );
  }
}

